﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;

namespace HRProject.Code.DAO
{
    public class RecordDAO : BaseDAO
    {
        public RecordDAO()
            : base ()
        {
        }

        public List<BLRecord> search(BLRecord filter)
        {
            List<BLRecord> result = new List<BLRecord>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@id", filter.Id,ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", filter.Name,ParameterDirection.Input));
            condition.Add(new BaseCondition("@userId", filter.UserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@typeId", filter.Type.Id, ParameterDirection.Input));
            DataTable data = select("usp_SearchRecords", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLRecord record = new BLRecord();
                record.Id = (Guid)data.Rows[i]["RecordId"];
                record.Name = (string)data.Rows[i]["RecordName"];
                record.UserId = (Guid)data.Rows[i]["UserId"];
                record.Type = new BLRecordType();
                record.Type.Id = (Guid)data.Rows[i]["RecordTypeId"];
                record.Type.Name = (string)data.Rows[i]["RecordTypeName"];
                record.Description = (string)data.Rows[i]["Description"];
                record.StartDate = (DateTime)data.Rows[i]["StartDate"];
                record.EndDate = (DateTime)data.Rows[i]["EndDate"];
                record.Rating = (byte)data.Rows[i]["Rating"];
                record.DataSignature = (long)data.Rows[i]["DataSignature"];
                record.CreateDate = (DateTime)data.Rows[i]["CreateDate"];
                record.CreateUserId = (Guid)data.Rows[i]["CreateUserId"];
                record.UpdateDate = (DateTime)data.Rows[i]["UpdateDate"];
                record.UpdateUserId = (Guid)data.Rows[i]["UpdateUserId"];
                result.Add(record);
            }
            return result;
        }

        public int insert(BLRecord record)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", Guid.NewGuid(), ParameterDirection.Input));
            condition.Add(new BaseCondition("@name",record.Name,ParameterDirection.Input));
            condition.Add(new BaseCondition("@userId", record.UserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@typeId", record.Type.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startDate", record.StartDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endDate", record.EndDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@rating", record.Rating, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", record.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@createUserId", Membership.GetUser().ProviderUserKey.ToString(),ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature",record.DataSignature,ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode",errorCode,ParameterDirection.Output));
            int result = update("usp_AddRecord", condition);
            return result;
        }

        public int update(BLRecord record)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", record.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", record.Name, ParameterDirection.Input));
            condition.Add(new BaseCondition("@typeId", record.Type.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startDate", record.StartDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endDate", record.EndDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@rating", record.Rating, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", record.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", Membership.GetUser().ProviderUserKey.ToString(), ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", record.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_UpdateRecord", condition);
            return result;
        }

        public int delete(BLRecord record)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", record.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", Membership.GetUser().ProviderUserKey.ToString(), ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", record.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_DeleteRecord", condition);
            return result;
        }

        public int deleteAllRecordsOfUser(Guid userId)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            long dataSignature = 0;
            condition.Add(new BaseCondition("@userId", userId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", Membership.GetUser().ProviderUserKey.ToString(), ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", dataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_DeleteAllRecordsOfUser", condition);
            return result;
        }
    }
}
